<template>
  
  <div >
    
  <el-table
    :data="pagedTableData"
    style="width: 100%">
    <el-table-column v-for="(item) in Object.keys(tableHeadData)" :key="item.bishe_id"
      :label="tableHeadData[item]"
      width="180">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{  scope.row[item]  }}</span>
      </template>
    </el-table-column>

    <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button
          class="my"
          size="mini"
          type="danger"
          @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
    
  <div class="my-paging">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="computedTotal">
    </el-pagination> 
  </div>
  
</div>
</template>

<script>
import axios from 'axios'




export default {
    data(){
        return{
            identity: 'student',

            submitData:{},
            //表头数据
            tableHeadData:{
                    bisheId: '毕设编号',
                    teacherId : '老师编号',
                    createtime: '课设上传时间',
                    univercity: '所属学院', 
                    major: '所属专业',
                    bisheName: '毕设标题',
                    content: '主要内容',
                    teacherName: '老师姓名',
                    teachersays: '老师献言',
                    ispass: '审核状态',
                    islock: '最终学生编号/已双向选择'
                    
            },

            //编辑数据
            message: [],

            labelPosition: 'right',
            formLabelAlign: {
              name: '',
              region: '',
              type: ''
            },
            tableData: [],
          currentPage: 1,
          pageSize: 10,
          

        }
    },
    computed:{
      computedTotal(){
        return this.tableData.length
      },
      pagedTableData() {
      const startIndex = (this.currentPage - 1) * this.pageSize;
      const endIndex = startIndex + this.pageSize;
      return this.tableData.slice(startIndex, endIndex);
      }
    },
    components:{
        

    },
    props:[],
    methods:{
        handleEdit(index, row) {
            console.log(index, row);
            this.message = []
            const h = this.$createElement;
          console.log(row.name);
          //要求：表头数据的key和tableData里的每一个数据对象的key相同
          Object.keys(this.tableHeadData).forEach(element => {
            const h1 = h('div', {style:{
                fontSize: '14px',
                marginTop: '8px',
                width: '100%',
                height: '30px',
                textAlign: 'center',
                lineHeight: '30px',
            }
              
            }, [
            h('div', {
              style:{
                width: 'auto',
                height: '30px',
                display: 'inline-block'
              }
            }, `${this.tableHeadData[element]}`),
            h('input', {
              attrs: {
                value: `${row[element]}`,
                
              },
              style:{
                marginLeft: '8px',
                float:'right',
                
              },
              
              on: {
                input: (event)=> {
                  // 在这里可以获取到输入框的值
                  let currentKey = element
                  let value = event.target.value
                  
                  this.submitData[currentKey] = value
                  
                  
                  
                }
            }}),
            
           
          ]);
            this.message.push(h1)
          });

           

        this.$msgbox({
          title: '编辑',
          message:  h('div', null,this.message) ,
          showCancelButton: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = '执行中...';
              setTimeout(() => {
                done();
                setTimeout(() => {
                  instance.confirmButtonLoading = false;
                }, 300);
              }, 2000);
            } else {
              done();
            }
          }
        }).then(action => {
           if (action === 'confirm') {
            const msg = this.handleConfirmAction(this.submitData,row); // 处理点击确定按钮后的逻辑
            this.$message({
              type: 'info',
              message: msg
            })
          } else {
            this.$message({
              type: 'info',
              message: '已取消修改'
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          });          
        })
            
            
        },
        handleDelete(index, row) {
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 数据库删除逻辑，需要 提交一个bishe_id
          axios.get(`/prod-api/bishe/del?bisheId=${row.bisheId}`)
              .catch(error => {
                    this.$router.push({name:'error', params:{error:error}})
                })

          this.$message({
            type: 'success',
            message: '删除成功!'
          });

          // 页面删除
          // this.tableData = this.tableData.filter(data=>data.id !== row.id)
          this.pagedTableData.splice(index, 1);
          
          this.tableData = this.tableData.filter(item => item.bisheId!==row.bisheId)
          

        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
            console.log(index,row);
            
            console.log(this.pagedTableData);
        },
        handleSizeChange(val) {
          this.pageSize = val;
        },
        handleCurrentChange(val) {
          this.currentPage = val;
        },
        handleConfirmAction(submitData,row){
          let obj
          obj = Object.assign({},row,submitData)
          
          axios.put(`/prod-api/bishe/upData`,obj)
                .then(response =>{
                  if(response.data.code === 0)
                    console.log(response.data);
                    return response.data.msg
                }).catch(error=>{
                   this.$router.push({name:'error', query:{error:error}})
                })

          location.reload(true)
        }
    },
    created(){
      axios.post('/prod-api/bishe/All')
            .then(response=>{
              this.tableData = response.data
              console.log(this.tableData);
            }).catch(error => {
                    this.$router.push({name:'error' , params:{error:error}})

                })
    }
}

</script>

<style>
.my-paging{
  text-align: center;
  margin-top: 20px;
}
.my{
  margin-left: 0px !important;
}
</style>